<?php


namespace app\admin\controller;
use app\common\lib\Util;
use app\common\model\Goods as articles;
use think\Request;

class Article extends Base
{
 public function index(articles $article){
     $article_data = $article->select();
     $this->assign('article_data',$article_data);
     return $this->fetch();
 }

 public function create(){
     return $this->fetch();
 }

 public function add(Request $request,articles $article){
     $article_data = [
         'classify' =>$request->classify,
         'worth' =>$request->worth,
         'status' =>$request->status,
         'weight_money' =>$request->weight_money,
         'remark'=>$request->remark,
     ];
     $model = $article->save($article_data);
     if(!$model){
         return Util::apiResponse([],'新增失败',-1);
     }
     return Util::apiResponse([],'新增成功',1);
 }

 public function edit($id,articles $article){
     $article_data = $article->find($id);
     $this->assign('article_data1',$article_data);
     return $this->fetch();
 }

 public function update($id,Request $request,articles $article){
     $article_data = [
         'classify' =>$request->classify,
         'worth' =>$request->worth,
         'status' =>$request->status,
         'weight_money' =>$request->weight_money,
         'remark'=>$request->remark,
     ];
     $model = $article->save($article_data,['id'=>$id]);
     if(!$model){
         return Util::apiResponse([],'新增失败',-1);
     }
     return Util::apiResponse([],'新增成功',1);
 }

 public function delete($id,articles $article){
     $exist = $article->find($id);
     if (!$exist) {
         return Util::apiResponse([], '该记录不存在', -1);
     }
     $res = $article->destroy($id);
     if(!$res){
         return Util::apiResponse([],'删除失败',-1);
     }
     return Util::apiResponse([],'',1);
 }

}